package com.firefly.app.cordova.plugins;

import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;

import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.firefly.mobile.app.config.Config;
import com.firefly.mobile.app.context.SytApplication;
import com.firefly.mobile.app.main.FireflyMobileActivity;
import com.firefly.mobile.app.util.SytLog;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;




public class SharePlugin  extends CordovaPlugin implements CordovaInterface{
	private final ExecutorService threadPool = Executors.newCachedThreadPool(); 
    /** 
     * 注意 构造方法不能为 
     *  
     * Plugin_intent(){} 
     *  
     * 可以不写或者 定义为如下 
     *  
     */  
    public SharePlugin() {  
    }  
    
    
    final SHARE_MEDIA[] displaylist = new SHARE_MEDIA[]
    {
        SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.QQ
//        ,SHARE_MEDIA.SINA
    };
	

  
    @Override  
    public boolean execute(final String action,final  org.json.JSONArray args,  
    		final  CallbackContext callbackContext) throws org.json.JSONException {  
        SytLog.i("进入统一plugin,sharePlugin", action);  
        
        cordova.getActivity().runOnUiThread(new Runnable() {
            public void run() {
                try {
//                	Looper.prepare();
					execPlugin(action, args, callbackContext);
//					Looper.loop();
				} catch (Exception e) {
					e.printStackTrace();
					SytLog.e("packPlugin", "插件调用出错");
					callbackContext.error("插件调用出错");
				}
            }
        });
        
        return true;  
  
    }  
	private static String text = "";
	private static String title= "" ;
	private static String targetUrl = Config.getString("share.url");; 
	private static String finalUrl = "";
	
	private static String app_logo = "https://m.winhc.cn/page/home/img/app.png";
	
	UMShareAPI mShareAPI = null;
    private void execPlugin(String action, org.json.JSONArray args,  
            CallbackContext callbackContext) throws org.json.JSONException {
	        if(action.equals("share")){
	        	text = args.getString(0);
	        	title = args.getString(1);
	        	String caseId = args.getString(2);
	        	finalUrl = targetUrl+"index.html#/caseProfileForShare/"+caseId;
	        	
	        	UMImage umImage = new UMImage(getActivity(), app_logo);
	            
	            UMWeb  web = new UMWeb(finalUrl);
	            web.setTitle(title);//标题
	            web.setThumb(umImage);  //缩略图
	            web.setDescription(text);//描述
	            
	            
	            new ShareAction(getActivity()).setDisplayList( displaylist )
	                    .withText( text )
	                    .withMedia(web)
	                    .setShareboardclickCallback(shareBoardlistener)
	                    .open();
	        }else if(action.equals("thirdLogin")){
//	        	if (mShareAPI == null) {
//	        		mShareAPI = UMShareAPI.get(SytApplication.context);
//				}
//	        	String platformText = args.getString(0);
//	        	SHARE_MEDIA platform = SHARE_MEDIA.WEIXIN; 
//	        	mShareAPI.doOauthVerify(getActivity(), platform, umAuthListener);
	        	
	        }else if(action.equals("shareForFriends")){
	        	text = args.getString(0);
	        	title = args.getString(1);
	        	String packet = args.getString(2);
	        	finalUrl = targetUrl+"shareForFrind.html"+"?packet="+packet;
	        	
	        	UMImage umImage = new UMImage(getActivity(), app_logo);
	            
	            UMWeb  web = new UMWeb(finalUrl);
	            web.setTitle(title);//标题
	            web.setThumb(umImage);  //缩略图
	            web.setDescription(text);//描述
	            
	            
	            new ShareAction(getActivity()).setDisplayList( displaylist )
	                    .withText( text )
	                    .withMedia(web)
	                    .setShareboardclickCallback(shareBoardlistener)
	                    .open();
	            
	            
	        }
    }
    
    

    
    private ShareBoardlistener shareBoardlistener = new ShareBoardlistener() {

		@Override
		public void onclick(SnsPlatform arg0, SHARE_MEDIA share_media) {
        		UMImage umImage = new UMImage(getActivity(), app_logo);

			 	UMWeb  web = new UMWeb(finalUrl);
	            web.setTitle(title);//标题
	            web.setThumb(umImage);  //缩略图
	            web.setDescription(text);//描述
	            
				 new ShareAction(getActivity()).setPlatform(share_media).setCallback(umShareListener)
	             .withText(text)
	             .withMedia(web)
	             .share();
				 
				 
			SytLog.i("分享点击回调");
		}
    };
    
    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(getActivity()," 分享成功啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(getActivity()," 分享失败啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(getActivity()," 分享已取消", Toast.LENGTH_SHORT).show();
        }

		@Override
		public void onStart(SHARE_MEDIA arg0) {
			// TODO Auto-generated method stub
			
		}
    };
    
	@Override
	public void startActivityForResult(CordovaPlugin command,
			Intent intent, int requestCode) {
		
	}

	@Override
	public void setActivityResultCallback(CordovaPlugin plugin) {
		
	}

	@Override
	public Activity getActivity() {
		return this.cordova.getActivity();
	}

	@Override
	public ExecutorService getThreadPool() {
		return threadPool;
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mShareAPI.onActivityResult(requestCode, resultCode, data);
    } 

	
	

}
